package io.renren.modules.biz.service.impl;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.ProjectConstant;
import io.renren.common.utils.Query;
import io.renren.common.validator.Assert;
import io.renren.modules.biz.dao.GrabPacketDao;
import io.renren.modules.biz.entity.CountDto;
import io.renren.modules.biz.entity.GrabPacketEntity;
import io.renren.modules.biz.entity.MemberEntity;
import io.renren.modules.biz.service.GrabPacketService;
import io.renren.modules.biz.service.MemberService;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

@Service("grabPacketService")
public class GrabPacketServiceImpl extends ServiceImpl<GrabPacketDao, GrabPacketEntity> implements GrabPacketService {

	@Autowired
	MemberService memberService;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		String mobile = (String) params.get("mobile");
		String memberId = (String) params.get("memberId");
		String startDate = (String) params.get("startDate");
		String endDate = (String) params.get("endDate");
		if (StringUtils.isBlank(memberId) && StringUtils.isNotBlank(mobile)) {
			MemberEntity entity = memberService.selectOne(new EntityWrapper<MemberEntity>().eq("mobile", mobile));
			if (entity == null || entity.getId() == null) {
				memberId = "-1";
			} else {
				memberId = entity.getId().toString();
			}
		}
		String sendPacketId = (String) params.get("sendPacketId");

		Page<GrabPacketEntity> page = this.selectPage(
				new Query<GrabPacketEntity>(params).getPage(),
				new EntityWrapper<GrabPacketEntity>().eq(StringUtils.isNotBlank(memberId), "grab_member_id", memberId)
						.gt(StringUtils.isNotBlank(startDate), "create_time", startDate).le(StringUtils.isNotBlank(endDate), "create_time", endDate)
						.eq(StringUtils.isNotBlank(sendPacketId), "send_packet_id", sendPacketId));

		return new PageUtils(page);
	}

	@Override
	public CountDto sumPacketAmount(String memberId, String mobile, String startDate, String endDate) {
		return baseMapper.sumPacketAmount(memberId, mobile, startDate, endDate);
	}

	@Override
	public double robotProfitByRoomId(Long roomId, Date startDate) {
		Assert.notNull(roomId, "roomId is null");
		BigDecimal profitSend = (BigDecimal) this.selectObj(new EntityWrapper<GrabPacketEntity>().setSqlSelect("ifnull(sum(cast(landmine_amount as decimal(18,4))),0)-ifnull(sum(cast(grab_amount as decimal(18,4))),0)").eq("is_robot", ProjectConstant.COMMON_NO)
				.ge(startDate != null, "create_time", startDate).where("send_packet_id in (select t1.id from tb_send_packet t1 where t1.is_robot=1 and t1.room_id={0})", roomId));

		BigDecimal profitGrab = (BigDecimal) this.selectObj(new EntityWrapper<GrabPacketEntity>().setSqlSelect("ifnull(sum(cast(grab_amount as decimal(18,4))),0)-ifnull(sum(cast(landmine_amount as decimal(18,4))),0)").eq("is_robot", ProjectConstant.COMMON_YES)
				.ge(startDate != null, "create_time", startDate).where("send_packet_id in (select t1.id from tb_send_packet t1 where t1.is_robot=0 and t1.room_id={0})", roomId));

		return profitSend.add(profitGrab).doubleValue();
	}
}
